package com.sinoyd.itszyw.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoyd.itszyw.Dialog.SelectDialog;
import com.sinoyd.itszyw.Model.RoutineInspectionInfo;
import com.sinoyd.itszyw.R;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * @createBy : Sinoyd 大饼洲
 * @description :
 * @projectName : ITYW
 * @creationDate: 2017-03-26 10:26
 */
public class RoutineInspectionAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<RoutineInspectionInfo> inspectionArray;
    private float rawX, rawY;
    private float xLeft, yTop;

    public RoutineInspectionAdapter(Context context, List<RoutineInspectionInfo> list) {
        this.inspectionArray = list;
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }


    private class viewHolder {
        @ViewInject(R.id.inspection_title)
        TextView inspection_title;
        @ViewInject(R.id.inspection_state)
        TextView inspection_state;
        @ViewInject(R.id.linear_inspection_state)
        LinearLayout linear_inspection_state;

    }

    @Override
    public int getCount() {
        return inspectionArray == null ? 0 : inspectionArray.size();
    }

    @Override
    public Object getItem(int i) {
        return inspectionArray.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        viewHolder cellHolder = null;

        if (view == null) {
            cellHolder = new viewHolder();
            view = mLayoutInflater.inflate(R.layout.listview_routine_inspection, null);
            x.view().inject(cellHolder, view);
            view.setTag(cellHolder);
        } else {
            cellHolder = (viewHolder) view.getTag();
        }
        final RoutineInspectionInfo inspectionInfo = inspectionArray.get(i);
        cellHolder.inspection_title.setText(inspectionInfo.title.replace("<br/>", "\n"));
        cellHolder.inspection_state.setText(inspectionInfo.state);
        final viewHolder finalCellHolder = cellHolder;

        cellHolder.linear_inspection_state.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        xLeft = event.getX();
                        yTop = event.getY();
                        rawX = event.getRawX();
                        rawY = event.getRawY();

                        Log.i("MainActivity", "event.getX()=" + xLeft + "," + "event.getY()=" + yTop + "," + "event.getRawX()=" + rawX + "event.getRawY()=" + rawY);
                        break;
                }
                return false;
            }
        });


        cellHolder.linear_inspection_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List resultList = new ArrayList<>();
                resultList.add("异常");
                resultList.add("正常");
//                resultList.add("网络");
//                resultList.add("硬件");
//                resultList.add("其他");
                SelectDialog resultDialog = new SelectDialog(mContext,resultList,R.style.dialog_no_dimenabled);

                resultDialog.setSelectInspectionResultListener(new SelectDialog.SelectInspectionResultListener() {
                    @Override
                    public void selectInspectionResult(String str, int id) {
                        finalCellHolder.inspection_state.setText(str);
                        inspectionInfo.state = str;
                        inspectionInfo.stateId = id + "";
                    }
                });
                resultDialog.show();

                //动态修改  dialog  大小  位置
                Window dialogWindow = resultDialog.getWindow();
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
                lp.x = (int) (rawX - xLeft); // 新位置X标
                lp.y = (int) (rawY- yTop) + 50; // 新位置Y坐标
                lp.width = mContext.getResources().getDimensionPixelSize(R.dimen.x108); // 宽度
                lp.height =LinearLayout.LayoutParams.WRAP_CONTENT; // 高度
                lp.alpha = 1.0f; // 透明度

                dialogWindow.setAttributes(lp);

            }
        });

        return view;
    }

}
